package com.funtl.framework.io;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class JXLExcel {
	/**
	 * 读取Excel文件
	 * @param filePath 文件路径
	 * @return Map类型，Key代表列名，Value为数组类型，该列的全部值
	 * @throws BiffException
	 * @throws IOException
	 */
	public static Map<String, String[]> readExcel(String filePath) throws BiffException, IOException {
		Map<String, String[]> map = null;
		Workbook book = Workbook.getWorkbook(new File(filePath));
		// 获得第一个工作表对象
        Sheet sheet = book.getSheet(0);
        if (sheet != null && sheet.getRows() > 0) {
        	map = new HashMap<String, String[]>();
        	for (int i = 0 ; i < sheet.getColumns() ; i++) {
        		//第i列，第0行为字段名称
        		Cell rowCell = sheet.getCell(i, 0);
        		String fieldName = rowCell.getContents();
        		String[] fieldValues = new String[sheet.getRows() - 1];
        		int index = 0;
        		for (int x = 1 ; x < sheet.getRows() ; x++) {
        			//第i列，第x行
        			Cell columnCell = sheet.getCell(i, x);
        			if (columnCell.getContents().trim().length() > 0) {
        				fieldValues[index] = columnCell.getContents().trim();
        				index++;
        			}
        		}
        		map.put(fieldName, fieldValues);
        	}
        }
        book.close();
		return map;
	}
	
	public static void main(String[] args) {
		try {
			Map<String, String[]> map = readExcel("D:/apache-tomcat-7.0.4/webapps/W951AD/temp/TianShenZhuan Android.xls");
			//获取Campaign这一列的内容
			String[] campaigns = map.get("Campaign");
			for (int i = 0 ; i < campaigns.length ; i++) {
				System.out.println(campaigns[i]);
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
